package com.wen.controller;

import com.github.pagehelper.PageInfo;
import com.wen.pojo.Role;
import com.wen.pojo.Users;
import com.wen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public ModelAndView findAll( @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                          @RequestParam(name = "size", required = true, defaultValue = "1") Integer size) throws Exception {
       ModelAndView model=new ModelAndView();
        List<Users> all = userService.findAll(page, size);
        PageInfo pageInfo = new PageInfo(all);
        model.addObject("pageInfo", pageInfo);
        model.setViewName("user-list");
        return model;
    }

    @RequestMapping("/add")
    public String add(Users users) throws Exception {
        userService.add(users);
        return "redirect:/user/findAll";
    }

    @RequestMapping("/findById")
    public ModelAndView findById( Integer id) throws Exception {
        ModelAndView model=new ModelAndView();
        Users byId = userService.findById(id);
        model.addObject("user", byId);
        model.setViewName("user-show");
        return model;
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id) throws Exception {
        userService.deleteById(id);
        return "redirect:/user/findAll";
    }

    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate( Integer id) throws Exception {
        ModelAndView model=new ModelAndView();
        model.addObject("user", userService.findById(id));
        model.setViewName("user-update");
        return model ;
    }

    @RequestMapping("/updateById")
    public String updateById(Users users) throws Exception {
        userService.updateById(users);
        return "redirect:/user/findAll";
    }

    /**
     * 查询用户以及用户可以添加的角色
     *
     * @param userId
     * @return
     */
    @RequestMapping("/findUserByIdAndAllRole")
    public ModelAndView findUserByIdAndAllRole( @RequestParam(name = "id") Integer userId) throws Exception {
        ModelAndView model=new ModelAndView();
       Users userInfo = userService.findById(userId);
        List<Role> otherRoles = userService.findOtherRoles(userId);
        model.addObject("user", userInfo);
        model.addObject("roleList", otherRoles);
        model.setViewName("user-role-add");
        return  model;
    }

    @RequestMapping("/addRoleToUser")
    public String addRoleToUser(Integer userId, @RequestParam(name = "ids") int[] roleIds) {
        userService.addRoleToUser(userId, roleIds);
        return "redirect:/user/findAll";
    }
}
